'use strict'
app.controller('ProjectController', function($scope, $state, $http, $timeout, time, obj, $stateParams) {

	$scope.api = $scope.serverUrl + "web/project";
    
    $scope.query = {
        "size": 10,
        "page": 1,
        "name": ""
    };
    
    $scope.form = {
        "id": '',
        "name": '',
        "remark": '',
        "label": '',
        "cover": '',
        "type": 0,
        "projectType": 0,
        "gitUrl": '',
        "gitUsername": '',
        "gitPassword": '',
        "localPath": ''
    };

    $scope.dialog = {
        "isAdd": true,
        "title": "新增",
    };

    $scope.body = {
        "queryTitle": "查询条件",
        "tableTitle": "数据列表"
    };

    $scope.total = 0;
    $scope.num = 0;
    $scope.projectArr = []


    let tbl;
    let layer = layui.layer;


    $timeout(function(){

    }, 0);

    $scope.list = function (){
        Object.keys($scope.query).forEach(function(key) {
            let val = $scope.query[key]
            if(val === ""){
                $scope.query[key] = undefined;
            }
        });
        $http.post($scope.api + "/page/list", $scope.query).then(function(res){
            if(res.data.code === 1){
                let data = res.data.data;
                $scope.total = data.total;
                let num = parseInt($scope.total / $scope.query.size);
                let mod = $scope.total % $scope.query.size;
                if(mod != 0){
                    num += 1;
                }
                $scope.num = num;
                $scope.projectArr = data.records;
            }else{
                layer.msg(res.data.msg, {icon: 5});
            }
        },
        function(err){
            layer.msg('接口请求异常', {icon: 5});
        });
    }

    $scope.list();

    $scope.info = function(entity){
        $state.go("app.projectInfo", {id: entity.id});
    }

    $scope.edit = function(entity){
        // console.log(entity)
        $scope.dialog.isAdd = false;
        $scope.dialog.title = '修改';
        obj.copy(entity, $scope.form);
        // $scope.$apply();
        $("#add").modal("show");
    }

    $scope.delete = function(id){
        $http.delete($scope.api + "/" + id, {}).then(function(res){
            if(res.data.code === 1){
                layer.msg('删除成功', {icon: 1});
            }else{
                layer.msg(res.data.msg, {icon: 5});
            }
            $scope.list();
        },
        function(err){
            layer.msg('接口请求异常', {icon: 5});
        });
    }

    $scope.add = function(){
        $scope.dialog.isAdd = true;
        $scope.dialog.title = '新增';
        $scope.form = {
            "id": '',
            "name": '',
            "remark": '',
            "label": '',
            "cover": '',
            "type": 0,
            "projectType": 0,
            "gitUrl": '',
            "gitUsername": '',
            "gitPassword": '',
            "localPath": ''
        };
        $("#add").modal("show");
    };

    $scope.search = function(){
        tbl.ajax.reload();
    };

    $scope.reset = function(){
        $scope.query = {
            "size": 10,
            "page": 1,
            "name": ""
        };
        tbl.ajax.reload();
    };

    $scope.save = function(){
        let url =  "/";
        if($scope.dialog.isAdd){
            $http.post($scope.api + url, $scope.form).then(function(res){
                if(res.data.code === 1){
                    layer.msg('操作成功', {icon: 1});
                }else{
                    layer.msg(res.data.msg, {icon: 5});
                }
                $scope.list();
                $("#add").modal("hide");
            },
            function(err){
                layer.msg('接口请求异常', {icon: 5});
            });
        }else{
            $http.put($scope.api + url, $scope.form).then(function(res){
                if(res.data.code === 1){
                    layer.msg('操作成功', {icon: 1});
                }else{
                    layer.msg(res.data.msg, {icon: 5});
                }
                $scope.list();
                $("#add").modal("hide");
            },
            function(err){
                layer.msg('接口请求异常', {icon: 5});
            });
        }
    };

    $scope.pre = function (){
        if($scope.query.page > 1){
            $scope.query.page -= 1;
            $scope.list();
        }
    }

    $scope.next = function (){
        if($scope.query.page < $scope.num){
            $scope.query.page += 1;
            $scope.list();
        }
    }

    $scope.uploadImg = function(event){
        //console.log(event);
        let param = new FormData();
        let files = event.target.files;
        for(let i = 0; i < files.length; i++){
            param.append("files", files[0]);
        }
        $http.post($scope.serverUrl + 'api/file/uploadFile', param, {
            transformRequest: angular.identity,
            headers: {'Content-Type': undefined}
        }).then(function(response){
            // console.log(response);
            $scope.form.entity.img = response.data.data;
        }, function(err){
            
        });
    };


});